package com.appkubes.mall.admin.application.customer.collect.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.customer.collect.entity.dto.GoodsCollectDTO;
import com.appkubes.mall.admin.application.customer.collect.entity.dto.CustomerCollectUpsertDTO;
import com.appkubes.mall.admin.application.customer.collect.entity.vo.CustomerCollectDelVO;
import com.appkubes.mall.admin.application.customer.collect.entity.vo.CustomerCollectGetVO;
import com.appkubes.mall.admin.application.customer.collect.entity.vo.CustomerCollectUpsertVO;
import com.appkubes.mall.admin.application.customer.collect.entity.vo.CustomerCollectVerifyVO;
import com.appkubes.mall.admin.application.customer.collect.service.CustomerCollectService;
import com.appkubes.mall.admin.application.customer.collect.entity.vo.GoodsCollectVO;
import com.appkubes.mall.core.controller.BaseMallCustomerCollectController;
import com.appkubes.mall.core.entity.MallCustomerCollect;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Lin Jiaming
 * @Date: 2020/3/4 15:51
 * @Description:
 */
@Api(tags = "渠道用户收藏")
@RestController
@RequestMapping("/customer/collect")
public class CustomerCollectController extends BaseMallCustomerCollectController {

    @Autowired
    CustomerCollectService customerCollectService;

    @ApiOperation("添加与修改收藏信息")
    @PostMapping("/upsert")
    public Response upsert(@RequestBody @ParamValid CustomerCollectUpsertVO collectUpsertVO) {
        CustomerCollectUpsertDTO upsertDTO = customerCollectService.upsert(collectUpsertVO);
        return Response.success(upsertDTO);
    }

    @ApiOperation("获取收藏信息")
    @GetMapping("/get")
    public  Response<MallCustomerCollect> get(@ParamValid CustomerCollectGetVO customerCollectGetVO) {
        MallCustomerCollect query = new MallCustomerCollect();
        BeanUtils.copyProperties(customerCollectGetVO, query);
        List<MallCustomerCollect> customerCollects = this.selectListByEntity(query);
        return Response.success(customerCollects);
    }

    @ApiOperation("删除收藏信息")
    @GetMapping("/del")
    public Response del(@ParamValid CustomerCollectDelVO delVO) {
        MallCustomerCollect customerCollect = new MallCustomerCollect();
        customerCollect.setCustomerKey(delVO.getCustomerKey());

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(customerCollect);
        wrapper.in("collect_key",delVO.getCollectKey());

        Boolean effectRow = this.remove(wrapper);
        return Response.success(effectRow);
    }

    @ApiOperation("验证目标Key是否收藏")
    @GetMapping("/verify")
    Response verify(@ParamValid CustomerCollectVerifyVO verifyVO) {
        QueryWrapper<MallCustomerCollect> queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer_key", verifyVO.getCustomerKey());
        queryWrapper.in("target_key", verifyVO.getTargetKey());
        List<MallCustomerCollect> collectList = this.getBaseMapper().selectList(queryWrapper);
        return Response.success(collectList);
    }

    @PostMapping("/goodsCollectList")
    @ApiOperation("商品收藏列表")
    public Response goodsCollectList(@ParamValid GoodsCollectVO goodsCollectVO) {
        Page pageInfo = new Page(goodsCollectVO.getPage(), goodsCollectVO.getSize());
        List<GoodsCollectDTO> goodsList = customerCollectService.goodsCollectList(goodsCollectVO.getCustomerKey(),
                goodsCollectVO.getSort(),
                pageInfo);
        return Response.success(goodsList);
    }
}
